package com.dragon.client.controller.auth;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.auth.IDictsortDetailService;
import com.dragon.repository.domain.dto.DictsortDetailDTO;
import com.dragon.repository.domain.vo.DictsortDetailVO;
import com.dragon.repository.entity.DictsortDetail;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 类型键值表 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-06-27
 */
@RestController
@RequestMapping("dictsortDetail")
@Api(tags = "字典键值列表")
public class DictsortDetailController {

    @Autowired
    IDictsortDetailService service;

    @GetMapping("list")
    @ApiOperation(value = "字典分类列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code",required = true,type = "int",value = "分类编码"),
            @ApiImplicitParam(name = "page",required = true,type = "int",value = "页码"),
            @ApiImplicitParam(name = "limit",required = true,type = "int",value = "条数")
    })
    public ResponseData<List<DictsortDetailDTO>> selectList(@RequestParam String code,@RequestParam Integer page, @RequestParam Integer limit){
        Page<DictsortDetail> list = service.selectPageList(code,page, limit);
        return ResponseData.success(DictsortDetailDTO.convertList(list.getRecords()), list.getTotal());
    }


    @PostMapping("add")
    @ApiOperation(value = "添加")
    public ResponseData addDetail(@RequestBody @Validated(Insert.class) DictsortDetailVO vo){
        return ResponseData.success(service.addDetail(DictsortDetailVO.convert(vo)));
    }


    @PostMapping("update")
    @ApiOperation(value = "修改")
    public ResponseData updateDetail(@RequestBody @Validated(Update.class) DictsortDetailVO vo){
        service.updateDetail(DictsortDetailVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除")
    public ResponseData deleteDetail(@RequestParam @ApiParam(required = true,name = "id",value = "id",type = "int") Integer id){
        service.deleteDetail(id);
        return ResponseData.success();
    }

    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除")
    public ResponseData deleteBatchDetail(@RequestParam @ApiParam(required = true,name = "ids",value = "id集合",type = "string") String ids){
        service.deleteBatchDetail(ids);
        return ResponseData.success();
    }

    @GetMapping("detailList")
    @ApiOperation(value = "通过字典类型获取队对应list")
    public ResponseData<List<DictsortDetailDTO>> selectDictSortDetail(@RequestParam @ApiParam(name = "code",value = "编码",required = true,type = "string") String code){
        return ResponseData.success(DictsortDetailDTO.convertList(service.selectDictSortDetail(code)));
    }


    @GetMapping("detailOne")
    @ApiOperation(value = "字典类型详情（通过code和值）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code",required = true,type = "string",value = "编码"),
            @ApiImplicitParam(name = "value",required = true,type = "int",value = "值")
    })
    public ResponseData<DictsortDetailDTO> selectDictSortDetailByCodeAndValue(
            @RequestParam String code,
            @RequestParam Integer value
    ) {
        return ResponseData.success(DictsortDetailDTO.convert(service.selectNameByCodeAndValue(code, value)));
    }
}
